#include "DisholQDeviceManagerWidget.h"

#include <assert.h>
#include <QtGui/QLayout>
#include <QtGui/QApplication>

#include "DisholQDeviceWidget.h"

namespace Dishol
{

DisholQDeviceManagerWidget::DisholQDeviceManagerWidget( QWidget* parent, Dishol::DeviceManager* deviceManager )
	:	QFrame(parent), 
		mDeviceManager(deviceManager),
		mDeviceListWidget(NULL),
		mStartStopButton(NULL)
{
	setWindowTitle( "Device Manager" );

	QVBoxLayout* mainLayout = new QVBoxLayout();
	setLayout( mainLayout);

	mDeviceListWidget = new QListWidget( this );
	mainLayout->addWidget( mDeviceListWidget );
	bool ret = false;
	ret = connect( mDeviceListWidget, SIGNAL( itemDoubleClicked(QListWidgetItem*) ), SLOT( onListItemDoubleCliked(QListWidgetItem*) ) );

	mTimer = new QTimer(this);
	ret = connect( mTimer, SIGNAL( timeout() ), SLOT( onTimerTimeOut() ) );
	assert( ret );
	mTimer->setInterval(0);
	mTimer->start();

	mDeviceManager->addListener(this);
}

DisholQDeviceManagerWidget::~DisholQDeviceManagerWidget()
{
	mDeviceManager->removeListener(this);
}

void DisholQDeviceManagerWidget::onTimerTimeOut()
{
	mDeviceManager->update();
}

Device* DisholQDeviceManagerWidget::getDeviceFromItem( QListWidgetItem* item ) const
{
	int devicePointer = item->data( 0x1234 ).toInt();
	assert(devicePointer);
	Device* device = reinterpret_cast<Device*>(devicePointer);
	return device;
}

QListWidgetItem* DisholQDeviceManagerWidget::getItemFromDevice( Device* device ) const
{
	for ( int i=0; i<mDeviceListWidget->count(); ++i )
	{
		QListWidgetItem* item = mDeviceListWidget->item(i);
		int devicePointer = item->data( 0x1234 ).toInt();
		assert(devicePointer);
		Device* d = reinterpret_cast<Device*>(devicePointer);
		if ( d==device )
			return item;
	}
	return NULL;
}

QDeviceWidget* DisholQDeviceManagerWidget::getDeviceWidgetFromItem( QListWidgetItem* item ) const
{
	Device* device = getDeviceFromItem(item);
	int devicePointer = reinterpret_cast<int>(device);
	QString objectName = QString("Device#%1").arg(devicePointer);
	QDeviceWidget* deviceWidget = findChild<QDeviceWidget*>(objectName);
	return deviceWidget;
}	

void DisholQDeviceManagerWidget::onListItemDoubleCliked( QListWidgetItem* item )
{
	QDeviceWidget* deviceWidget = getDeviceWidgetFromItem(item);
	if ( deviceWidget )
		return;

	Device* device = getDeviceFromItem(item);
	deviceWidget = new QDeviceWidget(this, device, Qt::Window);
	deviceWidget->setAttribute(Qt::WA_DeleteOnClose);
	int devicePointer = reinterpret_cast<int>(device);
	QString objectName = QString("Device#%1").arg(devicePointer);
	deviceWidget->setObjectName(objectName);
	QString title = QString("Device '%1'").arg( QString::fromUtf8(device->getName().c_str()) );
	deviceWidget->setWindowTitle( title );
	deviceWidget->resize( 640, 480 );	
	deviceWidget->show();
}

void DisholQDeviceManagerWidget::onDeviceAdded( DeviceManager* deviceManager, Device* device )
{
	updateDeviceListWidget();
}

void DisholQDeviceManagerWidget::onDeviceRemoving( DeviceManager* deviceManager, Device* device )
{
	QListWidgetItem* item = getItemFromDevice(device);
	QDeviceWidget* deviceWidget = getDeviceWidgetFromItem(item);
	if ( deviceWidget )
		delete deviceWidget;
}

void DisholQDeviceManagerWidget::onDeviceRemoved( DeviceManager* deviceManager, Device* device )
{
	updateDeviceListWidget();
}

void DisholQDeviceManagerWidget::updateDeviceListWidget()
{
	mDeviceListWidget->clear();

	Dishol::Devices devices = mDeviceManager->getDevices();
	for ( std::size_t i=0; i<devices.size(); ++i )
	{
		QString name = QString::fromUtf8( devices[i]->getName().c_str() );
		QListWidgetItem* item = new QListWidgetItem( name );
		mDeviceListWidget->addItem( item );
		Device* device = devices[i];
		int devicePointer = reinterpret_cast<int>(device);
		item->setData( 0x1234, devicePointer );
	}
}

}

